package com.example.pest.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.pest.mapper.UserMapper;
import com.example.pest.pojo.User;
import jakarta.servlet.http.HttpSession;
import jakarta.websocket.server.PathParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@CrossOrigin
public class UserController {

    @Autowired
    UserMapper userMapper;
//    @Autowired


    /*注册用户
     * */
    @PostMapping("/register")
    public String insertUser() {

        User user = new User();

        user.setId(1);
        user.setUsername("songliuguang");
        user.setPassword("123456");

        userMapper.insert(user);

        return "注册成功";

    }

    /*
     * 登录
     * */
    @GetMapping("/logintest")
    public  List<User> login(@PathParam("username")String username,@PathParam("password")String password) {
        String status="";
        int i=1;
//        User user =new User();
        List<User> all_user=userMapper.selectList(null);
        System.out.println(i++);



        return all_user;


    }





    @GetMapping("/logintest2/{username}/{password}")
    public  String loginok(@PathVariable("username") String username, @PathVariable("password") String password, HttpSession session) {

//        User user =new User();
        String status="";

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("username", username);
        queryWrapper.in("password", password);

        User user = userMapper.selectOne(queryWrapper); // 调用 selectOne 方法
        System.out.println("User: " + user);




        if (user != null) {
            status="success";
            System.out.println("==============================="+username);
            session.setAttribute("username", username);

            System.out.println("让我看看你："+session.getAttribute("username"));
        }
        else {
            status="fail";
        }


//        List<User> all_user=userMapper.selectList(null);
//        System.out.println(i++);
//        user.setUsername(username);
//        user.setPassword(password);


//        status=userMapper.selectOne(user);



        return status;


    }

    @GetMapping("/getuserinfo")
    public String getUserInfo(HttpSession session) {
        System.out.println("ttttttttttttttttttttttttt:"+session.getAttribute("username"));

        return session.getAttribute("username").toString();
    }

    @PostMapping("/register/{username}/{password}")
    public String register(@PathVariable("username") String username, @PathVariable("password") String password) {

        String status="";

        User user = new User();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date();
        String currentTime = sdf.format(date);

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("username", username);
        queryWrapper.in("password", password);


        User user2 = userMapper.selectOne(queryWrapper); // 调用 selectOne 方法


        if (user2 != null) {
            status="fail";
            return status;
        }
        else {
            user.setUsername(username);
            user.setPassword(password);
            user.setCreateTime(currentTime);
            return userMapper.insert(user)>0?"success":"fail";
        }



    }

    @GetMapping("/getAllUser/")
    public List<User> findUserByAll() {

        List<User> alluser= userMapper.selectList(null);
        System.out.println(alluser);
        return alluser;
    }

    /*
    更新用户信息
    * */

    @GetMapping("/updateUserInfo/{username}/{phone}/{address}")
    public String updateUserInfo(@PathVariable("username") String username, @PathVariable("phone") String phone, @PathVariable("address") String address,HttpSession session) {


        UpdateWrapper  updateWrapper = new UpdateWrapper();
        updateWrapper.in("username",username);
        updateWrapper.set("phone",phone);
        updateWrapper.set("adr",address);
        userMapper.update(updateWrapper);


        return "success";


    }
    


}
